package cn.gdut.chengcai.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gdut.chengcai.base.ConstantUtil;
import cn.gdut.chengcai.base.CourseUtil;
import cn.gdut.chengcai.base.JsonUtil;
import cn.gdut.chengcai.base.MessageUtil;
import cn.gdut.chengcai.base.ResponseData;
import cn.gdut.chengcai.entity.CourseInfo;
import cn.gdut.chengcai.entity.CourseInfoExample;
import cn.gdut.chengcai.param.BaseSearchParam;
import cn.gdut.chengcai.service.CourseInfoService;
import cn.gdut.chengcai.service.PartnerInfoService;
import cn.gdut.chengcai.vo.CourseInfoVo;

@Controller
public class MainController {

	@Autowired
	protected CourseInfoService courseInfoService;

	@Autowired
	protected PartnerInfoService partnerInfoService;

	/**
	 * 首页
	 */

	@RequestMapping("/index")
	public String index(HttpServletRequest request, HttpSession session, ModelMap map) {

		// 获取热门课程列表
		CourseInfoExample courseInfoExample = new CourseInfoExample();
		courseInfoExample.createCriteria().andIsDeletedEqualTo(false);
		courseInfoExample.setOrderByClause("view_count_ desc");
		List<CourseInfo> hotCourseList = courseInfoService.selectByExample(courseInfoExample);
		List<CourseInfoVo> hotCourseVoList = new ArrayList<>();

		try {
			List<CourseInfoVo> temp = CourseUtil.fillCourseVoList(hotCourseList, hotCourseVoList);
			if ((null != temp) && (temp.size() > 0)) {
				if (temp.size() >= 10) {
					hotCourseVoList = temp.subList(0, 10);
				} else {
					hotCourseVoList = temp.subList(0, temp.size());
				}
			}
			map.put("hotCourseList", hotCourseVoList);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "/index";
	}

	/**
	 * 登录页
	 */

	@RequestMapping("/login")
	public String login() {
		return "/login";
	}

	/**
	 * 注册页
	 */
	@RequestMapping("/register")
	public String register() {
		return "/register";
	}

	/**
	 * 重置页码操作
	 */
	@RequestMapping(value = "/resetPageAction", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData<Object> resetPageAction(@RequestBody String jsonString, HttpSession session, ModelMap map) {
		ResponseData<Object> result = new ResponseData<>();
		result.setIsSuccess(false);

		BaseSearchParam param = (BaseSearchParam) JsonUtil.fromJson(jsonString, BaseSearchParam.class);

		try {
			if (null != param.getPageNo()) {
				session.setAttribute(ConstantUtil.STR_PAGE_NO, param.getPageNo());
			} else {
				session.setAttribute(ConstantUtil.STR_PAGE_NO, "1");
			}
			result.setIsSuccess(true);
		} catch (Exception e) {
			result.setMessage(MessageUtil.SYSTEM_ERROR);
		}
		return result;
	}

}